package com.wyk.controller;

import java.util.Map;

import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;

@ControllerAdvice
public class ExceptionController {
	
	@ExceptionHandler(RuntimeException.class)
	@ResponseStatus(code=HttpStatus.INTERNAL_SERVER_ERROR)
	@ResponseBody
	public Map<String, Object>handlerRuntimeException(RuntimeException e){
		Map<String, Object>map =Maps.newHashMap();
		String error = Throwables.getStackTraceAsString(e);
		System.out.println(error);
		map.put("msg", "error");
		map.put("code", 100);
		return map;
	}
	
	
	@ExceptionHandler(Exception.class)
	@ResponseStatus(code=HttpStatus.INTERNAL_SERVER_ERROR)
	@ResponseBody
	public Map<String, Object>handlerException(RuntimeException e){
		Map<String, Object>map =Maps.newHashMap();
		String error = Throwables.getStackTraceAsString(e);
		System.out.println(error);
		map.put("msg", "error");
		map.put("code", 100);
		return map;
	}
}
